//
//  HomeViewController.m
//  DishMenu
//
//  Created by 成都千锋 on 15/10/21.
//  Copyright (c) 2015年 Yes. All rights reserved.
//

#import "HomeViewController.h"
#import "AppDelegate.h"
#import "TimeLable.h"
#import "MainTableViewCellTwo.h"
#import "MainTableViewCellOne.h"
#import "MainTableViewCellThere.h"
@interface HomeViewController()
{
    UIButton *leftBtn;
    UIButton *rightBtn;
    
    NSArray *headerName;//存放头部视图的文字
    
    CGFloat cellOneHeight;//第一个cell的高度
    CGFloat cellTwoHeight;//第二个cell的高度
    CGFloat cellThereHeight;//第三个cell的高度
    
    NSString *name;//当前是多少时间
    
    NSArray *dataArr;//分类的数组
}

@end

@implementation HomeViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadData];
    [self setData];
   
}
- (void)loadData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cateDan" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    dataArr = [NSJSONSerialization JSONObjectWithData:data options:1 error:nil][@"result"];
}
- (void)setData
{
    cellOneHeight = WIDTH / 4.0 * 0.618 * 3;
    cellThereHeight = WIDTH / 3.0;
    cellTwoHeight = WIDTH / 2.0;
    
    NSString *picName;
    
    NSDateComponents *dateCom = [MBFactory getNowTimeCom];
    if (dateCom.hour >= 0 && dateCom.hour <= 9) {
        picName = @"zao";
        name = @"早";
    }
    else if (dateCom.hour > 9 && dateCom.hour <= 15)
    {
        picName = @"zhong";
        name = @"中";
    }
    else
    {
        picName = @"wan";
        name = @"晚";
    }
    //设置头部名字
    headerName = @[@"分类",[NSString stringWithFormat:@"%@餐推荐",name],@"时段菜谱"];
   
    // 1、 设置导航栏的背景图片
    self.navigation_backgroundImageName = @"navigation_background";
    // 2、设置被拉伸图片view的高度
    self.stretchingImageHeight = 200;
    //得到应该显示那种图片
    TimeLable *time = [[NSBundle mainBundle] loadNibNamed:@"TimeLable" owner:nil options:nil].firstObject;
    time.frame = CGRectMake(0,60, 30, 60);
    [time setString:name];
//    [self.tableView.tableHeaderView addSubview:time];
    self.inTopView = time;
    // 3、设置头部拉伸图片的名称
    self.stretchingImageName = picName;
  
}
- (void)setupUI
{
    self.tableView.showsVerticalScrollIndicator = NO;
    leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [leftBtn setTitle:@"我的" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    [rightBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)leftBtnClick {
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.left.closed)
    {
        [tempAppDelegate.left openLeftView];
    }
    else
    {
        [tempAppDelegate.left closeLeftView];
    }
}


-(void)rightBtnClick {
    NSLog(@"rightClick");
}



#pragma mark - 数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 2)
    {
        MainTableViewCellTwo *cellTwo = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCellTwo" owner:nil options:nil].firstObject;
        for (NSInteger i = 2000; i < 2003; i ++) {
            UIButton *btn = (id)[cellTwo viewWithTag:i];
            [btn addTarget:self action:@selector(cellTwoClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cellTwo;
    }
    else if (indexPath.section == 0)
    {
        MainTableViewCellOne *cellOne = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCellOne" owner:nil options:nil].firstObject;
        [cellOne setData:dataArr];
        for (int index = 1000; index < 1012; index ++) {
            UIButton *btn = (id)[cellOne viewWithTag:index];
            [btn addTarget:self action:@selector(cellOneClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        return cellOne;
    }
    else
    {
        MainTableViewCellThere *cell = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCellThere" owner:nil options:nil].firstObject;
        [cell setData:name];
        return cell;
    }
}
- (void)cellOneClicked:(UIButton*)btn
{
    NSLog(@"%ld",(long)btn.tag);
}
- (void)cellTwoClicked:(UIButton*)btn
{
    NSLog(@"%ld",(long)btn.tag);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return cellOneHeight;
    } else if(indexPath.section == 1){
        return cellTwoHeight;
    }
    else
        return cellThereHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    label.text = headerName[section];
    label.textColor = [UIColor orangeColor];
    return label;
}

@end
